package com.hcbd.tdwt.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.hcbd.tdwt.R;
import com.hcbd.tdwt.ui.xingyu.vo.MenuIconVO;

@SuppressLint("ViewConstructor")
public class MoveImgLinearLayout extends LinearLayout {
    Context context;
    private View chiView;
    public int index=0;
    public String text;
   public MenuIconVO menuIconVO;
    public MoveImgLinearLayout(Context context, String text, Drawable drawable,View view,int index) {
        super(context);
        this.context=context;
        this.text=text;
        init(text,drawable,view);
    }
    public MoveImgLinearLayout(Context context,Drawable drawable,View view,MenuIconVO menuIconVO) {
        super(context);
        this.menuIconVO=menuIconVO;
        this.context=context;
        this.text=menuIconVO.menuName;
        this.index=menuIconVO.index;
        init(text,drawable,view);
    }
    public void init(String text, Drawable drawable,View view){
        LayoutInflater mInflater = LayoutInflater.from(context);
        chiView= mInflater.inflate(R.layout.icon_item,null,false);
        ImageView iconImg =chiView.findViewById(R.id.icon_img);
        TextView iconName =chiView.findViewById(R.id.icon_name);
        iconImg.setBackground(drawable);
        iconName.setText(text);
        addView(chiView);
        this.post(() -> {
            setX(view.getX());
            setY(view.getY());

        });

    }
    public void setIndex(int index){
        this.index=index;
    }
    float moveX;
    float moveY;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                moveX = event.getX();
                moveY = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                float x=getX() + (event.getX() - moveX);
                float y=getY() + (event.getY() - moveY);
                setTranslationX(x);
                setTranslationY(y);
                moveInterface.move(x,y);
                break;
            case MotionEvent.ACTION_UP:
                moveInterface.up();
                break;
        }

        return true;
    }
    public MoveInterface moveInterface;

    public void setMoveInterface(MoveInterface moveInterface) {
        this.moveInterface = moveInterface;
    }

    public interface MoveInterface {
        //注册结果回调
        void move(float x,float y);
        void up();
    }

    public void insertMenu(){
        menuIconVO.index=this.index;
        menuIconVO.menuName=this.text;
        if(index>7){
            menuIconVO.isShow=0;
        }else{
            menuIconVO.isShow=1;
        }
        MenuIconVO.upData(menuIconVO);
    }

}
